package com.inspur.incubator.controller;

import java.awt.TexturePaint;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.inspur.incubator.common.FileUtil;
import com.inspur.incubator.common.MD5Util;
import com.inspur.incubator.common.Tools;
import com.inspur.incubator.data.Menu;
import com.inspur.incubator.data.MhUser;
import com.inspur.incubator.service.RoleService;
import com.inspur.incubator.service.impl.MhUserImpl;

import net.sf.json.JSONArray;

@Controller
public class MhUserController {
	@Autowired
	private MhUserImpl mhUserImpl;
	@Autowired
	RoleService roleService;
	/**
	 * 登录方法
	 * @param mu
	 * @param session
	 * @return
	 */
	@RequestMapping("/sayHello")
	@ResponseBody
	public String sayHello() {
		return "Hello World";
	}
	
	@RequestMapping("/admin/dologin")
	@ResponseBody
	public String dologin(MhUser mu,HttpSession session){
		String userName=mu.getUserName();
		String userPass=mu.getUserPassword();
		String userYzm=mu.getUserPid();
		String yzm=(String) session.getAttribute("strCode");
		if (StringUtils.isNotEmpty(userYzm)&&StringUtils.isNotEmpty(userName)&&StringUtils.isNotEmpty(userPass)){
			if (!yzm.equals(userYzm.toUpperCase())) {
				return "error";
			}
			mu.setUserPassword(MD5Util.ecodeByMD5(userPass));
			MhUser mhUser = mhUserImpl.findUser(mu);
			//判断mhuser的类型（普通用户1、管理员0）角色
			//此处准备添加权限
		        if(mhUser!=null){
		        	session.setAttribute("mhUser", mhUser);
		            session.setAttribute("islogin","true");
		        	if("1".equals(mhUser.getUserType())){
		        		return "menhu/userInfo";
		        	}else if("0".equals(mhUser.getUserType())){
		        		List<Menu> roleList = roleService.getMemusInRole(mhUser.getRoleid());
		    			session.setAttribute("roleList", JSONArray.fromObject(roleList));
		    			System.out.println("拥有的权限为:"+roleList.toString());
		        		return "admin/index";
		        	}
		        	
		        }else{
		        	return "error";
		        }	
		}			
	    return "error";
	}
	/**
	 * 登录页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/admin/login")
	public ModelAndView loginlink(HttpServletRequest request,HttpSession session){
		ModelAndView mav = new ModelAndView("xixian_login");
		String randomNum = Tools.genRandomNum(5);
    	session.setAttribute("randomNum", randomNum);
    	mav.addObject(randomNum);
		return mav;
	}
	
	/**
	 * 注册页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/registerlink")
	public ModelAndView register(HttpServletRequest request,HttpSession session){
		ModelAndView mav = new ModelAndView("/views/test/registerlink");
		String randomNum = Tools.genRandomNum(5);
    	session.setAttribute("randomNum", randomNum);
    	mav.addObject(randomNum);
		return mav;
	}
	@RequestMapping(value="/admin/registerlinkdo",produces = "text/html;charset=utf-8")
	@ResponseBody
	public String registerlinkdo(MhUser mhUser,String randomNum,HttpSession session,
	@RequestParam("file") CommonsMultipartFile[] files,HttpServletRequest request) {
		String msg = "";
		MhUser mu = new  MhUser();
		mu.setUserName(mhUser.getUserName());
		mu = mhUserImpl.findUser(mu);
		if(mu!=null){
			session.removeAttribute("randomNum");
			msg = "该用名已被占用";
		}else{
			mhUser.setUserPassword(MD5Util.ecodeByMD5(mhUser.getUserPassword()));
			String[] arrStr =FileUtil.uploadFile(files,request);
			mhUser.setPictureUrl(arrStr[0]);
			int i = mhUserImpl.addUser(mhUser);
			if (i > 0) {
				session.removeAttribute("randomNum");
				msg = "注册成功";
			}else{
				msg = "注册失败";
			}
		}
		
		return Tools.toJson(msg);
	}
	
	 /**
     * 异步获取验证码
     * @param users
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/getrondomnum")
    public String getRondomNum(HttpSession session){
    	String randomNum = Tools.genRandomNum(5);
    	session.setAttribute("randomNum", randomNum);
    	return randomNum;
    }

}
